﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ProjectStorageLib;
using ProjectStorageDB;

public partial class controls_ProjectsGrid : System.Web.UI.UserControl
{

    #region Vars

    public delegate List<ProjectStorageLib.Project.ProjectManager> GetData();
    public event GetData GetDataEvent = null;

    public List<decimal> IncludedRows
    {
        get { return (List<decimal>)ViewState["IncludedRows"]; }
        set { ViewState["IncludedRows"] = value; }
    }

    private string CurSortExpression
    {
        get { return (string)ViewState["CurSortExpression"]; }
        set { ViewState["CurSortExpression"] = value; }
    }

    private SortDirection CurSortDirection
    {
        get { return ViewState["CurSortDirection"] == null ? SortDirection.Ascending : (SortDirection)ViewState["CurSortDirection"]; }
        set { ViewState["CurSortDirection"] = value; }
    }

    #endregion Vars

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();            
        }
    }
    public void ReloadData()
    {
        LoadData();
    }

    #region Private Methods

    private void LoadData()
    {
        try
        {
            List<ProjectStorageLib.Project.ProjectManager> data = null;

            if (GetDataEvent != null)
            {
                data = GetDataEvent();
                gvProjects.Visible = true;
                //load grid view
                gvProjects.DataSource = Utils.FromProjectManagerToDataTable(data);
                gvProjects.DataBind();

                //Init hiden field
                if (data != null)
                    hideColumn.LoadData(data.FirstOrDefault());
                else
                    hideColumn.Visible = false;
            }
        }
        catch (Exception err)
        {
        }
    }

   /// <summary>
   /// Used for sortin data 
   /// Do not sort data in db.
   /// </summary>
   /// <param name="e"></param>
   /// <param name="data"></param>
   /// <returns></returns>
    private DataTable SortData(GridViewSortEventArgs e, DataTable data)
    {
        if (e != null && e.SortExpression != "")
        {
            if (CurSortExpression == e.SortExpression)
            {
                if (CurSortDirection == SortDirection.Ascending)
                {
                    CurSortDirection = SortDirection.Descending;
                    data.DefaultView.Sort = e.SortExpression + " desc";
                }
                else
                {
                    CurSortDirection = SortDirection.Ascending;
                    data.DefaultView.Sort = e.SortExpression + " asc";
                }
            }
            else
                data.DefaultView.Sort = e.SortExpression + " asc";

            CurSortExpression = e.SortExpression;
        }
        return data;
    }

    protected void HideField()
    {
        //locals
        List<ProjectStorageLib.Project.ProjectManager> data = null;

        if (GetDataEvent != null)
        {
            data = GetDataEvent();

            if (data != null)
            {
                gvProjects.Visible = true;
                //load grid view
                gvProjects.DataSource = Utils.FromProjectManagerToDataTable(GetDataEvent(), hideColumn.hiddenFields);
                gvProjects.DataBind();
            }            
        }
    }

    #endregion Private Methods

    #region Grid Events

    protected void gvProjects_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            GridViewRow gvr = e.Row;
            switch (gvr.RowType)
            {
                case DataControlRowType.Header:

                    foreach (TableCell cell in e.Row.Cells)
                    {
                        if (cell.Controls.Count > 0)
                        {

                            LinkButton button = null;

                            foreach (Control c in cell.Controls)
                            {
                                if (c is LinkButton)
                                {
                                    button = (LinkButton)c;
                                    break;
                                }
                            }
                            if ((button != null))
                            {
                                if (CurSortExpression == button.CommandArgument)
                                {
                                    Image image = new Image();
                                    if (CurSortDirection == SortDirection.Ascending)
                                    {
                                        image.ImageUrl = ("~/App_Themes/Main/images/sort_arrow_up.png");
                                    }
                                    else
                                    {
                                        image.ImageUrl = ("~/App_Themes/Main/images/sort_arrow_down.png");
                                    }
                                    cell.Controls.Add(image);
                                }

                            }
                        }
                    }
                    break;                
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    protected void gvProjects_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (GetDataEvent != null)
        {
            gvProjects.Visible = true;
            //load grid view and sor data
            gvProjects.DataSource = SortData(e, Utils.FromProjectManagerToDataTable(GetDataEvent()));
            gvProjects.DataBind();
        }
    }

    protected void cb_CheckedChanged(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
        {
            if (IncludedRows == null)
                IncludedRows = new List<decimal>();
            IncludedRows.Add(Convert.ToDecimal(((CheckBox)sender).Attributes["IdProject"]));
        }
        else
        {
            if (IncludedRows.Contains(Convert.ToDecimal(((CheckBox)sender).Attributes["IdProject"])))
                IncludedRows.Remove(Convert.ToDecimal(((CheckBox)sender).Attributes["IdProject"]));
        }
    }
    #endregion Grid Events    
}